package edu.gdin.ecommerce.action;

import java.util.List;

import javax.annotation.Resource;

import net.sf.json.JSONArray;
import net.sf.json.JsonConfig;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.opensymphony.xwork2.ActionSupport;

import edu.gdin.ecommerce.model.CardIssuer;
import edu.gdin.ecommerce.model.Inventory;
import edu.gdin.ecommerce.service.ICardIssuerService;
import edu.gdin.ecommerce.service.IInventoryService;
import edu.gdin.ecommerce.util.Pager;

@Component("inventoryAction")
@Scope("prototype")
public class InventoryAction extends ActionSupport {

	private IInventoryService inventoryService;
	private ICardIssuerService cardIssuerService;
	private List<CardIssuer> cardIssuers;
	private Inventory inventory;
	private Pager pager;
	
	private String filePath;
	private String operTime1;
	private String operTime2;
	private Integer currentPage = 1;
	private String msg;

	//Adde by COF 
	//2012-04-30
	private String result;
	private String result2;
	
	public IInventoryService getInventoryService() {
		return inventoryService;
	}
	@Resource
	public void setInventoryService(IInventoryService inventoryService) {
		this.inventoryService = inventoryService;
	}
	public ICardIssuerService getCardIssuerService() {
		return cardIssuerService;
	}
	@Resource
	public void setCardIssuerService(ICardIssuerService cardIssuerService) {
		this.cardIssuerService = cardIssuerService;
	}
	public List<CardIssuer> getCardIssuers() {
		return cardIssuers;
	}
	public void setCardIssuers(List<CardIssuer> cardIssuers) {
		this.cardIssuers = cardIssuers;
	}
	public Inventory getInventory() {
		return inventory;
	}
	public void setInventory(Inventory inventory) {
		this.inventory = inventory;
	}
	public Pager getPager() {
		return pager;
	}
	public void setPager(Pager pager) {
		this.pager = pager;
	}
	public String getFilePath() {
		return filePath;
	}
	public void setFilePath(String filePath) {
		this.filePath = filePath;
	}
	public String getOperTime1() {
		return operTime1;
	}
	public void setOperTime1(String operTime1) {
		this.operTime1 = operTime1;
	}
	public String getOperTime2() {
		return operTime2;
	}
	public void setOperTime2(String operTime2) {
		this.operTime2 = operTime2;
	}
	public Integer getCurrentPage() {
		return currentPage;
	}
	public void setCurrentPage(Integer currentPage) {
		this.currentPage = currentPage;
	}
	public String getMsg() {
		return msg;
	}
	public void setMsg(String msg) {
		this.msg = msg;
	}
	

	public String getResult() {
		return result;
	}
	public void setResult(String result) {
		this.result = result;
	}
	
	
	public String getResult2() {
		return result2;
	}
	public void setResult2(String result2) {
		this.result2 = result2;
	}
	// ----------  methods  -----------
	

	public String preQueryInventory() {
		this.cardIssuers = this.cardIssuerService.getAllIssuer();
		return "preQueryInventory";
	}
	
	
	public String queryInventory() {
		this.cardIssuers = this.cardIssuerService.getAllIssuer();
		this.pager = this.inventoryService.queryInventory(this.inventory, this.operTime1, this.operTime2, this.currentPage);
		if(this.pager.getList().size() == 0) {
			this.addFieldError("msg", "没找到符合条件的库存卡！");
		}
		return "queryInventory";
	}
	
	
	/**
	 * Added by COF
	 * 2012-04-30
	 */
	public String queryInventoryAjax(){
		
		this.cardIssuers = this.cardIssuerService.getAllIssuer();
		this.inventory.setStatus(3);
		this.pager = this.inventoryService.queryInventory(this.inventory, this.operTime1, this.operTime2, this.currentPage);
		if(this.pager.getList().size() == 0) {
			this.addFieldError("msg", "没找到符合条件的库存卡！");
		}
		
		JsonConfig cfg = new JsonConfig();
		cfg.setExcludes(new String[]{"password","cardIssuer","inventoryDesc",""
				,"createTime","tracks2","tracks1","",
				"applicant","materialType","cardIssuerId"});
		
		result = JSONArray.fromObject(pager.getList(), cfg).toString();
		result2 = "{\"currentPage\":"+pager.getCurrentPage();
		result2 += ",\"totalSize\":"+pager.getTotalSize();
		result2 += ",\"totalPage\":"+pager.getTotalPage();
		result2 += "}";
		
		return "success";
	}
	

	
	public String exportByQuery() {
		try {
			this.filePath = this.inventoryService.exportByQuery(this.inventory, this.operTime1, this.operTime2);
		} catch (Exception e) {
			this.addFieldError("msg", e.getMessage());
			this.preQueryInventory();
			return "preQueryInventory";
		}
		return "exportByQuery";
	}
	

}
